{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0631c124-d39c-4d1b-ac1d-f303025d6add",
   "metadata": {
    "libroCellType": "text"
   },
   "source": [
    "<p><br /></p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c043615d-8739-49b1-925c-88d2b4c023ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import random\n",
    "from typing import DefaultDict\n",
    "from tqdm import tqdm\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from collections import defaultdict\n",
    "import json\n",
    "from random import sample\n",
    "\n",
    "def select_overlap_user(train_name,save_train_name,overlap_ratio):\n",
    "    data = pd.read_csv(train_name)\n",
    "    user_node = data['user_id'].tolist()\n",
    "    seq_d1 = data['seq_d1'].tolist()\n",
    "    seq_d2 = data['seq_d2'].tolist()\n",
    "    domain_id = data['domain_id'].tolist()\n",
    "    user_node_overlap,seq_d1_overlap, seq_d2_overlap, domain_id_overlap  = [], [], [], []\n",
    "    user_node_nolap,seq_d1_nolap, seq_d2_nolap, domain_id_nolap  = [], [], [], []\n",
    "    for i in range(len(user_node)):\n",
    "        seq1_tmp = json.loads(seq_d1[i])\n",
    "        seq2_tmp = json.loads(seq_d2[i])\n",
    "        if len(seq1_tmp)!=0 and len(seq2_tmp)!=0:\n",
    "            user_node_overlap.append(user_node[i])\n",
    "            seq_d1_overlap.append(seq1_tmp)\n",
    "            seq_d2_overlap.append(seq2_tmp)\n",
    "            domain_id_overlap.append(domain_id[i])\n",
    "        else :\n",
    "            user_node_nolap.append(user_node[i])\n",
    "            seq_d1_nolap.append(seq1_tmp)\n",
    "            seq_d2_nolap.append(seq2_tmp)\n",
    "            domain_id_nolap.append(domain_id[i])\n",
    "    print(len(user_node_overlap),len(user_node_nolap)) # 3384 69945\n",
    "    #nolap_num = int(len(user_node_overlap)/overlap_ratio-len(user_node_overlap)) # 3384 + \n",
    "    sample_nolap_num = int(len(user_node_nolap)*overlap_ratio)\n",
    "    idx_lst = [i for i in range(len(user_node_nolap))]\n",
    "    select_idx = sample(idx_lst, sample_nolap_num)\n",
    "    print(sample_nolap_num)\n",
    "    # print(select_idx)\n",
    "    for idx_tmp in select_idx:\n",
    "        user_node_overlap.append(user_node_nolap[idx_tmp])\n",
    "        seq_d1_overlap.append(seq_d1_nolap[idx_tmp])\n",
    "        seq_d2_overlap.append(seq_d2_nolap[idx_tmp])\n",
    "        domain_id_overlap.append(domain_id_nolap[idx_tmp])\n",
    "        # user_node_nolap.append(user_node_overlap[idx_tmp])\n",
    "        # seq_d1_nolap.append(seq_d1_overlap[idx_tmp])\n",
    "        # seq_d2_nolap.append(seq_d2_overlap[idx_tmp])\n",
    "        # domain_id_nolap.append(domain_id_overlap[idx_tmp])\n",
    "    # print(len(user_node_nolap))\n",
    "    dataframe = pd.DataFrame({'user_id':user_node_overlap,'seq_d1':seq_d1_overlap,'seq_d2':seq_d2_overlap,'domain_id':domain_id_overlap})\n",
    "    dataframe.to_csv(save_train_name,index=False,sep=',')\n",
    "\n",
    "# all_name = \"/ossfs/workspace/CDSR/amazon_dataset/cloth_sport_all.csv\" # name change in next use\n",
    "# data = pd.read_csv(all_name).set_index(['user_id'],drop=False).sample(frac=1.0)#.reset_index(drop=True)\n",
    "# train_len = int(data.shape[0] * 0.80)\n",
    "# save_data_train = data.iloc[ : train_len]\n",
    "# save_data_val = data.iloc[ train_len: ]\n",
    "train_name = \"/ossfs/workspace/CDSR/amazon_dataset_oldnodr/phone_elec_train100.csv\"\n",
    "# val_name = \"/ossfs/workspace/CDSR/amazon_dataset/cloth_sport_test.csv\"\n",
    "# save_data_train.to_csv(train_name, index=False)\n",
    "# save_data_val.to_csv(val_name, index=False)\n",
    "\n",
    "ratios = [0.25,0.75]\n",
    "for overlap_ratio in ratios:\n",
    "# overlap_ratio = 0.75\n",
    "# #game video 90 refine\n",
    "    save_train_name = \"/ossfs/workspace/CDSR/amazon_dataset/phone_elec_train\"+str(int(overlap_ratio*100))+\".csv\"\n",
    "    # # save_val_name = \"/ossfs/workspace/MRHG/amazon/music_movie_test\"+str(int(overlap_ratio*100))+\".csv\"\n",
    "    select_overlap_user(train_name,save_train_name,overlap_ratio)\n",
    "# select_overlap_user(val_name,save_val_name,overlap_ratio)\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
